import Vue from 'vue'
import VueRouter from 'vue-router'
// 导入store
import store from "@/store/index";
import indexRouters from './modules/Index'
import myRouters from './modules/My'
import rationalRouters from './modules/Rational'
import searchRouters from './modules/Search'
import CommonRouters from './modules/Common'
import destinationRouters from './modules/Destination'
import CommonRoutes from "./modules/Common"
import processRoutes from './modules/process'

Vue.use(VueRouter)

const routes = [
    ...indexRouters,
    ...myRouters,
    ...rationalRouters,
    ...searchRouters,
    ...CommonRouters,
    ...destinationRouters,
    ...CommonRoutes,
    ...processRoutes,
]



const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

const originalPush = VueRouter.prototype.push; 
VueRouter.prototype.push = function push(location) { 
  return originalPush.call(this, location).catch((err) => err); 
};
//路由守卫
router.beforeEach((to, from, next) => {
  // 需要定义一组数据（路由），这组路由可能是需要登录才能访问的（当然也可能是不需要登录就能访问的），当我们获取到目标路由的时候，可以去数组中进行判断，符合特定条件就继续，否则不允许访问。
  // 例如：下面的数组需要登录才能访问
  let needLogin = ["/settingup","/order",'/custom'];
  // } else {
      if (needLogin.includes(to.path)) {
          // 判断是否有token
          let token = store.state.my._token;
          if (token) {
              // 登录了
              next();
          } else {
              // 没登录，去登录页面
              router.push("/login?goto=" + to.path);
          }
      } else {
          // 继续
          next();
      }
});
export default router
